package com.sdy.healthcode.web.controller.company;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.model.ResumptionApply;
import com.sdy.healthcode.biz.service.ResumptionApplyService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

@Slf4j
@RestController
@RequestMapping("/resumption")
@Api(tags = "复工申请相关接口（完善中）")
public class ResumptionController extends BaseController{

    @Autowired
    private ResumptionApplyService applyService;

    public static String randomNum() {
        // 获取随机申请订单
        long dateEx = System.currentTimeMillis();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timeEx = dateFormat.format(dateEx);
        int aEx = (int) (Math.random() * 900 + 100);
        String randomThree = Integer.toString(aEx);
        String randomNum = timeEx + randomThree;
        return randomNum;
    }

    @GetMapping("/getApplyInfo")
    @ApiOperation(value = "获取当前申请单信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "businessType", value = "商企类型(0-企业，1-商超)"),
            @ApiImplicitParam(name = "approvalResult", value = "状态"),
            @ApiImplicitParam(name = "createTime", value = "申请时间"),
            @ApiImplicitParam(name = "companyName", value = "名称")
    })
    public Response<IPage<ResumptionApply>> getApplyInfo(HttpServletRequest request, ResumptionApply resumptionApply, Page page) {
        LambdaQueryWrapper<ResumptionApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != resumptionApply.getBusinessType(), ResumptionApply::getBusinessType, resumptionApply.getBusinessType())
                .eq(null != resumptionApply.getApprovalResult(), ResumptionApply::getApprovalResult, resumptionApply.getApprovalResult())
                .eq(null != resumptionApply.getCreateTime(), ResumptionApply::getCreateTime, resumptionApply.getCreateTime())
                .eq(StringUtil.isNotBlank(resumptionApply.getCompanyName()), ResumptionApply::getCompanyName, resumptionApply.getCompanyName())
                .orderByDesc(ResumptionApply::getCreateTime);
        IPage result = applyService.page(page, wrapper);
        return Response.success(result);
    }

    @PostMapping("/approval")
    @ApiOperation(value = "复工审批", response = ResumptionApply.class)
    public Response<Boolean> approval(@RequestBody
    @ApiParam(name = "ResumptionApply", value = "实体类,放入主键id + 审批结果(0-审批中，1-通过，2-驳回)") ResumptionApply res) throws BizException {
        // 检验是否已经被审批
        ResumptionApply resumptionApply = applyService.getOne(Wrappers.<ResumptionApply>lambdaQuery()
                .eq(ResumptionApply::getId, res.getId()));
        Integer approvalResult = resumptionApply.getApprovalResult();
        Assert.isTrue(approvalResult != 0, "请勿重复审批!");
        LambdaUpdateWrapper<ResumptionApply> upWrapper = new LambdaUpdateWrapper<>();
        upWrapper.eq(ResumptionApply::getId, res.getId())
                .set(ResumptionApply::getApprovalResult, res.getApprovalResult());
        boolean update = applyService.update(upWrapper);
        Assert.isTrue(!update, "审批出错!");
        return Response.success(update);
    }
}
